﻿/*
Copyright (c) 2011 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#region Usings
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using ObjectCartographer.Interfaces;
using ObjectCartographer.Mappings.DataClasses;
#endregion

namespace ObjectCartographer.Mappings
{
    /// <summary>
    /// Maps two objects together
    /// </summary>
    /// <typeparam name="Source">Source type</typeparam>
    /// <typeparam name="Destination">Destination type</typeparam>
    public class Mapping<Source,Destination>:IMapping
    {

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        public Mapping()
        {
            Setup();
        }

        #endregion

        #region IMapping Members

        public Type SourceType { get { return typeof(Source); } }

        public Type DestinationType { get { return typeof(Destination); } }

        public int NumberObjectsExpected()
        {
            return Mappings.Count;
        }

        public void Sync(object SourceObject, object DestinationObject)
        {
            if (SourceObject.GetType() == SourceType)
            {
                for (int y = 0; y < Mappings.Count; ++y)
                {
                    object SourceValue = Utilities.Reflection.Reflection.GetPropertyValue(SourceObject, Mappings[y].Source);
                    Utilities.Reflection.Reflection.SetValue(SourceValue, DestinationObject, Mappings[y].Destination, Mappings[y].Format);
                }
                return;
            }
            for (int y = 0; y < Mappings.Count; ++y)
            {
                object DestinationValue = Utilities.Reflection.Reflection.GetPropertyValue(SourceObject, Mappings[y].Destination);
                Utilities.Reflection.Reflection.SetValue(DestinationValue, DestinationObject, Mappings[y].Source, Mappings[y].Format);
            }
        }

        public object Create(object Source)
        {
            if (Source.GetType() == SourceType)
            {
                object DestinationObject = typeof(Destination).Assembly.CreateInstance(typeof(Destination).FullName);
                Sync(Source, DestinationObject);
                return DestinationObject;
            }
            object SourceObject = typeof(Destination).Assembly.CreateInstance(typeof(Source).FullName);
            Sync(Source, SourceObject);
            return SourceObject;
        }

        #endregion

        #region Protected Functions

        /// <summary>
        /// Maps a source property to a destination property
        /// </summary>
        /// <param name="SourceExpression">Source property</param>
        /// <param name="DestinationExpression">Destination property</param>
        protected void Map(Expression<Func<Source, object>> SourceExpression,
            Expression<Func<Destination, object>> DestinationExpression)
        {
            Map(SourceExpression, DestinationExpression, "");
        }

        /// <summary>
        /// Maps a source property to a destination property
        /// </summary>
        /// <param name="SourceExpression">Source property</param>
        /// <param name="DestinationExpression">Destination property</param>
        /// <param name="Format">The string format that the destination should use</param>
        protected void Map(Expression<Func<Source, object>> SourceExpression,
            Expression<Func<Destination, object>> DestinationExpression,
            string Format)
        {
            Setup();
            string SourceName = Utilities.Reflection.Reflection.GetPropertyName<Source>(SourceExpression);
            string DestinationName = Utilities.Reflection.Reflection.GetPropertyName<Destination>(DestinationExpression);
            for (int x = 0; x < Mappings.Count; ++x)
            {
                if (Mappings[x].Destination == DestinationName)
                {
                    Mappings.RemoveAt(x);
                    break;
                }
            }
            for (int x = 0; x < Mappings.Count; ++x)
            {
                if (Mappings[x].Source == SourceName)
                {
                    Mappings.RemoveAt(x);
                    break;
                }
            }
            Mappings.Add(new MappingInfo(SourceName, DestinationName, Format, ""));
        }

        /// <summary>
        /// Removes a mapping
        /// </summary>
        /// <param name="SourceExpression">Source property to ignore</param>
        protected void Ignore(Expression<Func<Source, object>> SourceExpression)
        {
            Setup();
            string SourceName = Utilities.Reflection.Reflection.GetPropertyName<Source>(SourceExpression);
            for (int x = 0; x < Mappings.Count; ++x)
            {
                if (Mappings[x].Source == SourceName)
                {
                    Mappings.RemoveAt(x);
                    break;
                }
            }
        }

        /// <summary>
        /// Removes a mapping
        /// </summary>
        /// <param name="DestinationExpression">Destination property to ignore</param>
        protected void Ignore(Expression<Func<Destination, object>> DestinationExpression)
        {
            Setup();
            string DestinationName = Utilities.Reflection.Reflection.GetPropertyName<Destination>(DestinationExpression);
            for (int x = 0; x < Mappings.Count; ++x)
            {
                if (Mappings[x].Destination == DestinationName)
                {
                    Mappings.RemoveAt(x);
                    break;
                }
            }
        }

        #endregion

        #region Private Functions

        /// <summary>
        /// Sets up the mapping
        /// </summary>
        private void Setup()
        {
            if (Mappings == null)
            {
                Mappings = new List<MappingInfo>();
                PropertyInfo[] Properties = typeof(Source).GetProperties();
                Type DestinationType = typeof(Destination);
                for (int x = 0; x < Properties.Length; ++x)
                {
                    if (DestinationType.GetProperty(Properties[x].Name) != null)
                    {
                        Mappings.Add(new MappingInfo(Properties[x].Name, Properties[x].Name, "", ""));
                    }
                }
            }
        }

        #endregion

        #region Protected Variables

        internal List<MappingInfo> Mappings { get; set; }

        #endregion
    }
}
